<template>
	<el-container>
		<el-aside width="23%">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model.trim="treeFilterText" clearable />
				</el-header>
				<el-main class="nopadding">
					<el-tree
						ref="treeRef"
						class="menu"
						node-key="id"
						:data="treeData"
						:current-node-key="''"
						:highlight-current="true"
						:expand-on-click-node="false"
						:filter-node-method="treeFilterNode"
						accordion
						@node-click="treeClick">
					</el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button
						icon="el-icon-plus"
						type="primary"
						:disabled="!reqParams.processId"
						@click="open_detail" />
				</div>
				<div class="right-panel">
					<div class="right-panel-search"></div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable
					ref="tableRef"
					:api-obj="this.$API.procprocessparams.page"
					:params="reqParams"
					:column="column"
					:initData="false"
					row-key="id"
					stripe
					hideContextMenu>
					<el-table-column align="center" fixed="right" label="操作" width="140">
						<template #default="scope">
							<el-button text type="primary" size="small" @click="open_dialog(scope.row)">编辑</el-button>
							<el-divider direction="vertical" />
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</template>
					</el-table-column>

					<template #isDisabled="{ data }">
						<el-tag :type="data.isDisabled ? danger : 'success'">
							{{ data.isDisabled ? '禁用' : '启用' }}
						</el-tag>
					</template>
				</scTable>
			</el-main>

			<modify ref="modify" @complete="complete" />
			<tag-detail ref="tagDetailRef" @complete="complete" />
		</el-container>
	</el-container>
</template>

<script>
import modify from "./modify";
import tagDetail from "./tagDetail";

export default {
    components: {
        modify,
        tagDetail,
    },
    data() {
        return {
            treeFilterText: '',
            treeData: [],
            reqParams: {
                processId: null,
                procCode: '',
                opCode: '',
                lineCode: '',
            },
            column: [
                { prop: "id", label: "id", width: 100, hide: true },
                { prop: 'procCode', label: '工序编号', width: 100 },
                { prop: 'productCode', label: '产品编号', width: 140 },
                { prop: 'tagName', label: '标记', width: 240 },
                { prop: 'tagDisplayName', label: '标记名称', width: 120 },
                { prop: 'step', label: '步序', width: 80 },
                { prop: 'stdLower', label: '下限值', width: 100 },
                { prop: 'stdHigher', label: '上限值', width: 100 },
                { prop: 'isDisabled', label: '启用状态', width: 80 },
                { prop: 'createTime', label: '创建时间', width: 140 },
            ],
        };
    },
    mounted() {
        this.initTree();
    },
    watch: {
        treeFilterText: {
            handler(newVal) {
                this.$refs.treeRef.filter(newVal);
            },
        },
    },
    methods: {
        //树过滤
        treeFilterNode(value, data) {
            if (!value) return true;
            return data.label.includes(value);
        },
        async initTree() {
            const resp = await this.$API.procprocess.tree.get();
            if (resp.code != 200) {
                this.$message.error(resp.message);
                return;
            }
            this.treeData = resp.data;
        },
        treeClick(node) {
            // 非叶子节点，清空数据
            if (!node.isLeaf) {
                this.reqParams.processId = null;
                this.reqParams.procCode = '';
                this.reqParams.opCode = '';
                return;
            }

            this.reqParams.processId = node.id;
            this.reqParams.procCode = node.value;
            this.reqParams.opCode = node.data;
            this.reqParams.lineCode = node.parentValue;

            this.$refs.tableRef.refresh();
        },
        complete() {
            this.$refs.tableRef.refresh();
        },
        open_detail() {
            this.$refs.tagDetailRef.open(this.reqParams);
        },
        open_dialog(row) {
            this.$refs.modify.open(row);
        },
    }
}
</script>

<style scoped></style>
